﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using TuKu.PPS;
using TuKu.MM;
using TuKu.Mop;

namespace TuKu
{
    public partial class MainForm : Form
    {
        TopicList topiclist;
        TopicAdd topicadd;
        TemplateMain templatemain;
        CategorySub categorysub;
        CategoryMain categorymain;
        ppsForm ppsform;
        ppsSubList ppssubList;
        TestForm testform;
        ppsTemplate ppstemplate;
        ppsdownload download;
        ppsPage ppspage;
        ppsAdlist ppsadlist;
        ppslist _ppslist;
        ppsAdd ppsadd;
        mopPage moppage;
        public MainForm()
        {
            InitializeComponent();
        }
        public void minWindows()
        {
            this.WindowState = FormWindowState.Minimized;
        }
        private void ToolStripMenuItemTopic_Click(object sender, EventArgs e)
        {
            //CreateFormInstance("TuKu.TopicAdd");
            if (topicadd == null || topicadd.IsDisposed)
            {
                topicadd = new TopicAdd();
                topicadd.MdiParent = this;
            }
            topicadd.Show();
            ActiveFrm(topicadd, this);
            toolStripStatusWindowsName.Text = topicadd.Text;
        }

        private void ToolStripMenuItemSubGroup_Click(object sender, EventArgs e)
        {
            //CreateFormInstance("TuKu.CategorySub");
            if (categorysub == null || categorysub.IsDisposed)
            {
                categorysub = new CategorySub();
                categorysub.MdiParent = this;
            }
            categorysub.Show();
            ActiveFrm(categorysub, this);
            toolStripStatusWindowsName.Text = categorysub.Text;
        }
        private void ToolStripMenuItemGroup_Click(object sender, EventArgs e)
        {
            //CreateFormInstance("TuKu.CategoryMain");
            if (categorymain == null || categorymain.IsDisposed)
            {
                categorymain = new CategoryMain();
                categorymain.MdiParent = this;
            }
            categorymain.Show();
            ActiveFrm(categorymain, this);
            toolStripStatusWindowsName.Text = categorymain.Text;

        }

        private void ToolStripMenuItemtopiclist_Click(object sender, EventArgs e)
        {
            //CreateFormInstance("TuKu.TopicList");
            if (topiclist == null || topiclist.IsDisposed)
            {
                topiclist = new TopicList();
                topiclist.MdiParent = this;
            }
            topiclist.Show();
            ActiveFrm(topiclist, this);
            toolStripStatusWindowsName.Text = topiclist.Text;
            
        }
        private void CreateFormInstance(string formName)
        {
            Assembly assembly = Assembly.LoadFrom(ConfigurationManager.AppSettings["dllpath"]);
            Type t = assembly.GetType(formName); //formName一定要是[命名空间.窗体名]
            object obj = Activator.CreateInstance(t);
            Form frm = (Form)obj;
            frm.MdiParent = this;
            frm.Show();
            ActiveFrm(frm, this);
            toolStripStatusWindowsName.Text = frm.Text;
        }
        /// <summary>
        /// 加载子窗体的时候，调整子窗体的Icon显示问题
        /// </summary>
        /// <param name="tobeActive">需要加载的子窗体</param>
        /// <param name="Mdi">主窗体：Mdi窗体</param>
        public static void ActiveFrm(Form tobeActive, Form Mdi)
        {
            for (int i = 0; i < Mdi.MdiChildren.Length; i++)
            {
                if (Mdi.MdiChildren.GetValue(i).Equals(tobeActive))
                {
                    tobeActive.WindowState = FormWindowState.Normal;
                }
            }
            tobeActive.WindowState = FormWindowState.Maximized;
        }

        private void ToolStripMenuItemTemplate_Click(object sender, EventArgs e)
        {
            if (templatemain == null || templatemain.IsDisposed)
            {
                templatemain = new TemplateMain();
                templatemain.MdiParent = this;
            }
            templatemain.Show();
            ActiveFrm(templatemain, this);
            toolStripStatusWindowsName.Text = templatemain.Text;
        }

        private void 网页读取ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (ppsform == null || ppsform.IsDisposed)
            {
                ppsform = new ppsForm();
                ppsform.MdiParent = this;
            }
            ppsform.Show();
            ActiveFrm(ppsform, this);
            toolStripStatusWindowsName.Text = ppsform.Text;
        }

        private void 分类处理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (ppssubList == null || ppssubList.IsDisposed)
            {
                ppssubList = new ppsSubList();
                ppssubList.MdiParent = this;
            }
            ppssubList.Show();
            ActiveFrm(ppssubList, this);
            toolStripStatusWindowsName.Text = ppssubList.Text;
        }

        private void 数据下载ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (testform == null || testform.IsDisposed)
            {
                testform = new TestForm();
                testform.MdiParent = this;
            }
            testform.Show();
            ActiveFrm(testform, this);
            toolStripStatusWindowsName.Text = testform.Text;
        }

        private void 模板管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //ppstemplate.cs
            if (ppstemplate == null || ppstemplate.IsDisposed)
            {
                ppstemplate = new ppsTemplate();
                ppstemplate.MdiParent = this;
            }
            ppstemplate.Show();
            ActiveFrm(ppstemplate, this);
            toolStripStatusWindowsName.Text = ppstemplate.Text;
        }

        private void 首页模块管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (download == null || download.IsDisposed)
            {
                download = new ppsdownload();
                download.MdiParent = this;
            }
            download.Show();
            ActiveFrm(download, this);
            toolStripStatusWindowsName.Text = download.Text;
        }

        private void 页面生成ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (ppspage == null || ppspage.IsDisposed)
            {
                ppspage = new ppsPage();
                ppspage.MdiParent = this;
            }
            ppspage.Show();
            ActiveFrm(ppspage, this);
            toolStripStatusWindowsName.Text = ppspage.Text;
        }

        private void 推荐列表管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (ppsadlist == null || ppsadlist.IsDisposed)
            {
                ppsadlist = new ppsAdlist();
                ppsadlist.MdiParent = this;
            }
            ppsadlist.Show();
            ActiveFrm(ppsadlist, this);
            toolStripStatusWindowsName.Text = ppsadlist.Text;
        }

        private void 列表页处理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (_ppslist == null || _ppslist.IsDisposed)
            {
                _ppslist = new ppslist();
                _ppslist.MdiParent = this;
            }
            _ppslist.Show();
            ActiveFrm(_ppslist, this);
            toolStripStatusWindowsName.Text = _ppslist.Text;
        }

        private void 添加项目ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (ppsadd == null || ppsadd.IsDisposed)
            {
                ppsadd = new ppsAdd();
                ppsadd.MdiParent = this;
            }
            ppsadd.Show();
            ActiveFrm(ppsadd, this);
            toolStripStatusWindowsName.Text = ppsadd.Text;

        }

        private void 处理数据ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //mopPage.cs
            if (moppage == null || moppage.IsDisposed)
            {
                moppage = new mopPage();
                moppage.MdiParent = this;
            }
            moppage.Show();
            ActiveFrm(moppage, this);
            toolStripStatusWindowsName.Text = moppage.Text;
        }



    }
}
